<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>throttle</title>
</head>
<body>
<script>
//防抖
const debounce = (fn, time = 1000) => {
	let flag = true;
	return function() {
		if(!flag) {
			return;
		}
		flag = false;
		setTimeout(() => {
			fn.apply(this, arguments);
			flag = true;
		}, time);
	};
};

var fn1 = function(x) {
	console.log(x);
};
var fn2 = debounce(fn1);
fn2(1);
fn2(2);
setTimeout(() => {
	fn2(3);
}, 1200);
</script>
<script type="text/javascript">
// 节流
function throttle (fn, wait) {
	let prev = new Date();
	return function() {
		let now = new Date();
		let timeT = (now.getTime() - prev.getTime()) / 1000;
		if(timeT > wait) {
			fn.apply(this, arguments);
			prev = new Date();
		}
	}
}
var fn3 = throttle(fn1, 2);
fn3(44);
setTimeout(() => {
	fn3(55);
}, 3000)
</script>
</body>
</html>